//
//  SHTabBarView.m
//  ShinyOrder
//
//  Created by 王方伟 on 16/7/22.
//  Copyright © 2016年 ShinyHub. All rights reserved.
//

#import "SHTabBarView.h"
#import "SHTabBarButton.h"
#import "XMGMiddleView.h"
#import "SHManHeader.h"

@interface SHTabBarView ()
@property (nonatomic, weak) SHTabBarButton *selectButton;
@property (nonatomic, strong) NSMutableArray *tabBarArray;
@property (weak, nonatomic) XMGMiddleView *middleView;
@end

@implementation SHTabBarView
-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self=[super initWithCoder:aDecoder]) {
        [self initView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self initView];
    }
    return self;
}

-(NSMutableArray *)tabBarArray{
    if (_tabBarArray==nil) {
        _tabBarArray=[NSMutableArray array];
    }
    return _tabBarArray;
}

#pragma mark 设置TabBar的背景
-(void)initView{
    self.backgroundColor = [UIColor whiteColor];
}
    
    
- (void)setIsAddCenterButton:(BOOL)isAddCenterButton{
    _isAddCenterButton = isAddCenterButton;
    if(isAddCenterButton){
        [self addCenterView];
    }
}

- (void)addCenterView{
    if(_middleView == nil){
        XMGMiddleView *middleView = [XMGMiddleView shareInstance];
        [self addSubview:middleView];
        _middleView = middleView;
    }
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    _selectedIndex = selectedIndex;
    SHTabBarButton *button = self.tabBarArray[_selectedIndex];
    for (SHTabBarButton *button in self.tabBarArray) {
        button.selected = NO;
    }
    button.selected = YES;
}

- (void)setMiddleClickBlock:(void (^)(BOOL))middleClickBlock{
    self.middleView.middleClickBlock = middleClickBlock;
}

-(void)addTabBarButtonWith:(UITabBarItem *)barItem{
    SHTabBarButton *btn=[[SHTabBarButton alloc]init];
    btn.item=barItem;
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:btn];
    //将TabBar添加到数组中
    [self.tabBarArray addObject:btn];
    //设置默认第一个被选中
    if (self.tabBarArray.count==1) {
        [self buttonClick:btn];
    }
}

-(void)buttonClick:(SHTabBarButton *)button{
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]) {
        [self.delegate tabBar:self didSelectedButtonFrom:self.selectButton.tag to:button.tag];
    }
    self.selectButton.selected=NO;
    button.selected=YES;
    self.selectButton=button;
}

#pragma mark 设置按钮的位置
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat tabBarH = self.frame.size.height;
    CGFloat tabBarW = self.frame.size.width;
    CGFloat btnH = tabBarH - SafeAreaBottomHeight;
    CGFloat btnW = tabBarW/self.subviews.count;
    CGFloat btnY = 0;
    if (_isAddCenterButton){
        //设置中间按钮的位置
        self.middleView.center = CGPointMake(tabBarW*0.5, tabBarH*0.5);
    }
    for (int index=0; index<self.tabBarArray.count; index++) {
        SHTabBarButton *btn=self.tabBarArray[index];
        CGFloat btnX=btnW*index;
        if (_isAddCenterButton){
            if (index>1) {
                btnX+=btnW;
            }
        }
        btn.frame=CGRectMake(btnX, btnY, btnW, btnH);
        //绑定Tag
        btn.tag=index;
    }
}

@end
